Getting a list of files which contain the matching text
//Setup the search parameters
SearchParms parms = new SearchParms();
//This is the directory to search
parms.DirectoriesToSearch.Add(@"c:\_git");
//Include all C# files by wildcard
parms.FilesToInclude.Add("*.cs");
parms.FileIncludeSearchType = FileSearchType.Wildcard;
//Search for a plain text phrase
parms.SearchString = "public partial class";
parms.SearchExpressionType = SearchExpressionType.PlainText;
//Search with the parameters
FileSearchLogic logic = new FileSearchLogic();
List<string> results = logic.SearchFiles(parms);
foreach (var filePath in results)
{
Console.WriteLine(filePath);
}
Getting a list of locations in a file which contain the matching text
//Set the file to load
string filePath = @"C:\_git\Compare-Net-Objects\Compare-NET-Objects-Tests\TestClasses\Invoice.cs";
//What to search in the file
SearchParms searchParms = new SearchParms();
searchParms.SearchString = "FirstName";
searchParms.SearchExpressionType = SearchExpressionType.PlainText;
//Load the matches for the file
LoadFileMatchesLogic loadFileMatchesLogic = new LoadFileMatchesLogic();
LoadFileResult loadFileResult = loadFileMatchesLogic.LoadFileMatches(searchParms, filePath);
foreach (var matchLocation in loadFileResult.MatchLocations)
{
Console.WriteLine($"{matchLocation.MatchText}: Start {matchLocation.StartPosition}, Length {matchLocation.Length}");
}
Creating and Searching an index
//Setup the search parameters
SearchParms searchParms = new SearchParms();
//This is the directory to index
searchParms.DirectoriesToSearch.Add(@"c:\_git");
//Include all C# files by wildcard
searchParms.FilesToInclude.Add("*.cs");
searchParms.FileIncludeSearchType = FileSearchType.Wildcard;
//Set the directory to save the index
searchParms.IndexPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "FileSearchLibrarySearchIndexExample");
//Create the index
IndexLogic indexLogic = new IndexLogic();
indexLogic.CreateIndexIfItDoesNotExist(searchParms);
//Specify what to search
searchParms.SearchString = "public partial class";
searchParms.SearchExpressionType = SearchExpressionType.PlainText;
//List the files that are found
List<string> files = indexLogic.SearchIndex(searchParms);
foreach (string file in files)
{
Console.WriteLine(file);
}